package com.example.mvp.view.imgselector.album;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.example.baselibrary.R;
import com.example.mvp.view.SKTopBar;
import com.example.mvp.view.imgselector.album.previewimage.ImageDetailFragment;
import com.example.mvp.view.imgselector.base.ImageSelectorActivity;
import com.example.mvp.view.imgselector.model.AlbumRepository;
import com.example.mvp.view.imgselector.utils.ActivityUtils;
import com.githang.statusbar.StatusBarCompat;


public class AlbumActivity extends ImageSelectorActivity {

    public static final String TAG = AlbumActivity.class.getSimpleName();

    private AlbumPresenterImage mAlbumPresenter;
    private SKTopBar jtb_aa;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album);

        initViews();
        AlbumFragment albumFragment;
        ImageDetailFragment imageDetailFragment;
        //内存重启时调用   解决 内存重启时可能发生的Fragment重叠异常
        if (savedInstanceState != null) {
            imageDetailFragment = (ImageDetailFragment) getSupportFragmentManager().findFragmentByTag(ImageDetailFragment.TAG);

            albumFragment = (AlbumFragment) getSupportFragmentManager().findFragmentByTag(AlbumFragment.TAG);


            if (imageDetailFragment != null && albumFragment != null) {
                getSupportFragmentManager().beginTransaction()
                        .hide(albumFragment)
                        .show(imageDetailFragment)
                        .commit();
            }
        } else {
            //创建AlbumFragment

            albumFragment = AlbumFragment.newInstance();
            ActivityUtils.addFragmentToActivity(
                    getSupportFragmentManager(), albumFragment, AlbumFragment.TAG, false);

        }
        AlbumRepository albumRepository = AlbumRepository.getInstance(this);
        //创建AlbumPresenter
        mAlbumPresenter = new AlbumPresenterImage(
                albumRepository,
                getSupportLoaderManager(),
                albumFragment);
    }


    private void initViews() {

        jtb_aa = findViewById(R.id.jtb_aa);
        jtb_aa.setTitle(getResources().getString(R.string.string_album));
        jtb_aa.setEventInterface(new SKTopBar.EventInterface() {
            @Override
            public void leftOnClick() {
                onBackPressed();

            }

            @Override
            public void rightOnClick() {
                mAlbumPresenter.returnResult();
            }
        });
    }

    public void setSubmitBtnText(CharSequence text, boolean enabled) {
        if (jtb_aa==null){
            return;
        }

        if (enabled){
            jtb_aa.getRightTV().setVisibility(View.VISIBLE);
        }else{
            jtb_aa.getRightTV().setVisibility(View.GONE);
        }

        jtb_aa.getRightTV().setText(text.toString());
    }

    public void setToolbarTitle(CharSequence title) {
        jtb_aa.setTitle(title.toString());
    }

    @Override
    public void onBackPressed() {
        AlbumFragment albumFragment =
                (AlbumFragment) getSupportFragmentManager().findFragmentByTag(AlbumFragment.TAG);

        if (albumFragment == null) {
            super.onBackPressed();
            return;
        }

        if (albumFragment.isHidden()) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .show(albumFragment)
                    .commit();
        } else if (albumFragment.mFab.getVisibility() != View.VISIBLE) {
            albumFragment.hideFolderList();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAlbumPresenter.clearCache();
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
    }
}
